<template>
    <div class="views-dingdanjiaoyi-updt">
        <div>
            <el-card class="box-card">
                <template #header>
                    <div class="clearfix">
                        <span class="title"> 添加 </span>
                    </div>
                </template>

                <el-form :model="form" ref="formModel" label-width="140px" status-icon validate-on-rule-change>
                    <el-form-item v-if="isRead" label="订单号" prop="dingdanhao"> {{ form.dingdanhao }} </el-form-item>

                    <el-form-item v-if="isRead" label="订单商品" prop="dingdanshangpin">
                        <div id="dataListdingdanshangpin" style="text-align: left">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>商品编号</th>
                                        <th>商品名称</th>
                                        <th>商品分类</th>
                                        <th>商品图片</th>
                                        <th>价格</th>
                                        <th>发布人</th>
                                        <th>购买数量</th>
                                        <th>小计</th>
                                        <th>购买人</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr v-for="dataMap in dataSets">
                                        <td>{{ dataMap.shangpinbianhao }}</td>
                                        <td>{{ dataMap.shangpinmingcheng }}</td>
                                        <td><e-select-view module="shangpinfenlei" :value="dataMap.shangpinfenlei" select="id" show="fenleiming"></e-select-view></td>
                                        <td>
                                            <div style="width: 100%; max-width: 120px">
                                                <e-img :src="dataMap.shangpintupian" type="list"></e-img>
                                            </div>
                                        </td>
                                        <td>{{ dataMap.jiage }}</td>
                                        <td>{{ dataMap.faburen }}</td>
                                        <td>{{ dataMap.goumaishuliang }}</td>
                                        <td>{{ dataMap.xiaoji }}</td>
                                        <td>{{ dataMap.goumairen }}</td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </el-form-item>

                    <el-form-item v-if="isRead" label="商家" prop="shangjia"> {{ form.shangjia }} </el-form-item>

                    <el-form-item v-if="isRead" label="购买人姓名" prop="goumairenxingming"> {{ form.goumairenxingming }} </el-form-item>

                    <el-form-item v-if="isRead" label="购买人" prop="goumairen"> {{ form.goumairen }} </el-form-item>

                    <el-form-item label="文件" prop="wenjian" required :rules="[{required:true, message:'请填写文件'}]">
                        <e-upload-file v-model="form.wenjian"></e-upload-file>
                    </el-form-item>

                    <el-form-item label="图片" prop="tupian" required :rules="[{required:true, message:'请填写图片'}]">
                        <e-upload-images v-model="form.tupian" is-paste></e-upload-images>
                    </el-form-item>

                    <el-form-item label="备注" prop="beizhu"> <el-input type="textarea" v-model="form.beizhu"></el-input> </el-form-item>

                    <el-form-item v-if="btnText">
                        <el-button type="primary" @click="submit">{{ btnText }}</el-button>
                    </el-form-item>
                </el-form></el-card
            >
        </div>
    </div>
</template>

<script setup>
    import http from "@/utils/ajax/http";
    import DB from "@/utils/db";
    import rule from "@/utils/rule";
    import router from "@/router";

    import { ref, reactive, computed, watch } from "vue";
    import { useRoute } from "vue-router";
    import { session } from "@/utils/utils";
    import { ElMessage, ElMessageBox } from "element-plus";
    import { useDingdanjiaoyiFindById, canDingdanjiaoyiFindById, canDingdanjiaoyiUpdate, canShangpindingdanFindById } from "@/module";
    import { extend } from "@/utils/extend";

    const route = useRoute();
    const props = defineProps({
        id: [String, Number],
        btnText: {
            type: String,
            default: "保存",
        },
        isRead: {
            type: Boolean,
            default: true,
        },
        isHouxu: {
            type: Boolean,
            default: true,
        },
    });
    const form = useDingdanjiaoyiFindById(props.id);
    const emit = defineEmits(["success"]);
    const formModel = ref();
    const loading = ref(false);

    const submit = () => {
        formModel.value.validate().then((res) => {
            if (loading.value) return;
            loading.value = true;
            canDingdanjiaoyiUpdate(form).then(
                (res) => {
                    loading.value = false;
                    if (res.code == 0) {
                        emit("success", res.data);
                        if (props.isHouxu) {
                            ElMessage.success("更新成功");
                            router.go(-1);
                        }
                    } else {
                        ElMessageBox.alert(res.msg);
                    }
                },
                (err) => {
                    loading.value = false;
                    ElMessageBox.alert(err.message);
                }
            );
        });
    };

    const readMap = reactive({});
    watch(
        () => form.shangpindingdanid,
        (id) => {
            canShangpindingdanFindById(id).then((res) => {
                extend(readMap, res);
            });
        }
    );
    const dataSets = DB.name("shangpindingdandingdanshangpin").where("shangpindingdanid", route.query.id).order("id asc").selectRef();
</script>

<style scoped lang="scss">
    .views-dingdanjiaoyi-updt {
    }
</style>
